<template>
  <div class="app-container">
    <el-form :inline="true">
      <el-form-item label="">
        <el-input v-model="queryParams.dictName" @keyup.enter.native="handleQuery('1')" placeholder="请输入字典名称" />
      </el-form-item>
      <el-form-item label="">
        <el-input v-model="queryParams.dictType" @keyup.enter.native="handleQuery('1')" placeholder="请输入字典类型" />
      </el-form-item>
      <el-form-item label="">
        <el-date-picker
          v-model="dateRange"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="至"
          start-placeholder="创建开始日期"
          end-placeholder="创建结束日期"
        />
      </el-form-item>
      <el-form-item label="" style="width: 90px">
        <el-select v-model="queryParams.status" placeholder="状态">
          <el-option
            v-for="item in statusOptions"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery('1')">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="handleQuery('reset')">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb5">
      <el-col :span="1.5"><el-button type="primary" icon="el-icon-plus" @click="handleAdd">新增</el-button></el-col>
      <el-col :span="1.5"><el-button type="success" :disabled="single" icon="el-icon-edit" @click="handleUpdate(null)">修改</el-button></el-col>
      <el-col :span="1.5"><el-button type="danger" :disabled="multiple" icon="el-icon-delete" @click="handleDelete(null)">删除</el-button></el-col>
    </el-row>

    <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange">
      <el-table-column type="selection" />
      <el-table-column prop="dictName" label="字典名称" min-width="80px" />
      <el-table-column label="字典类型" min-width="80px">
        <template slot-scope="scope">
          <router-link :to="'/dict/type/data/' + scope.row.dictType ">
            <el-button type="text">{{ scope.row.dictType }}</el-button>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="开启状态" min-width="80px">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.status" active-value="1" inactive-value="0" @change="handleStatusChange(scope.row)" />
        </template>
      </el-table-column>
      <el-table-column prop="remark" label="备注" min-width="80px" />
      <el-table-column prop="createTime" label="创建时间" />
      <el-table-column label="操作" min-width="120px">
        <template slot-scope="scope">
          <el-button type="text" size="mini" icon="el-icon-edit" @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button type="text" size="mini" icon="el-icon-delete" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="handleQuery" />

    <el-dialog :visible.sync="dialogVisible" :title="title" width="450px">
      <el-form ref="edit" :model="form" :label-width="formLabelWidth">
        <el-form-item label="字典名称">
          <el-input v-model="form.dictName" />
        </el-form-item>
        <el-form-item label="字典类型">
          <el-input v-model="form.dictType" />
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio v-for="item in statusOptions" :key="item.dictValue" :label="item.dictValue">{{ item.dictLabel }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="form.remark" type="textarea" :rows="4" />
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button type="primary" @click="submitForm">确定</el-button>
        <el-button @click="cancel">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import initData from '@/mixins/initData'
import { changeDictTypeStatus, addDictType, updateDictType } from '@/api/system/dict'

export default {
  mixins: [initData],
  data() {
    return {
      formLabelWidth: '80px',
      statusOptions: [],
      // formReset: {
      //   status: '1'
      // },
      queryParams: {
        dictName: undefined,
        dictType: undefined,
        status: undefined
      }
    }
  },
  mounted() {
    this.handleQuery()
    this.getDict('status_normal').then(res => {
      this.statusOptions = res.data
    })
  },
  methods: {
    beforeInit() {
      this.base = '/system/dict/type'
      this.modalName = '数据字典'
    },
    // 修改状态
    handleStatusChange(row) {
      const text = row.status === '0' ? '停用' : '启用'
      this.$confirm('确认要' + text + '【' + row.dictName + '】字典吗？', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return changeDictTypeStatus(row.id, row.status)
      }).then(res => {
        this.msgSuccess(text + '成功')
      }).catch(() => {
        row.status = row.status === '0' ? '1' : '0'
      })
    },
    submitForm() {
      console.log('submit')
      this.$refs['edit'].validate(valid => {
        if (valid) {
          if (!this.form.id) {
            addDictType(this.form).then(() => this.msgSuccess('添加成功'))
          } else {
            updateDictType(this.form).then(() => this.msgSuccess('修改成功'))
          }
          this.dialogVisible = false
          this.handleQuery()
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
